home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15117 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.3 KB  |  100 lines

  1. Path: mxsld2.pd.infn.it!LORETI
  2. From: loreti@mxsld2.pd.infn.it (Maurizio Loreti)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: how to get weeknumber according calendar
  5. Date: Wed, 17 Apr 1996 11:36:02 GMT
  6. Organization: I.N.F.N. Padova - CDF/CMS VAXcluster
  7. Message-ID: <4l2hhi$1a5k@serra.unipi.it>
  8. References: <4jt3jr$j58@hdxx05.telecom.ptt.nl>,<4kavnk$aqe@blue.usps.gov>
  9. Reply-To: loreti@mxsld2.pd.infn.it
  10. NNTP-Posting-Host: mxsld2.pd.infn.it
  11.  
  12. In article <4kavnk$aqe@blue.usps.gov>, Bill Seymour <wseymour@email.usps.gov> writes:
  13. >a.broers@ptt-telecom.unisource.nl (Andre Broers) wrote:
  14. >>Hello all,
  15. >>
  16. >>Hopw can I get a weeknumber following the calendar. This means the
  17. >>first days in januari can be week 52, 53 or 1 (and not 0).
  18. >>
  19. >>Is there an algoritm to calculate this??
  20. >
  21. >int weeknumber(struct tm *ptm)
  22. >{
  23. >    return ptm->tm_yday / 7 + 1;
  24. >}
  25.  
  26. Obviously you did not care to read the question, did you?  when it
  27. refers to the first days in January.
  28.  
  29. Also, weeks start in sundays or mondays; how do you take care of that
  30. in your code?
  31.  
  32. Here is something I wrote a couple years ago; hope that helps... 
  33. However the first days in January are week 1; you can modify the code,
  34. it is in the public domain :-)
  35. ---------------------------------Begin file: week.c
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38.  
  39. #define isleap(year) (((year) % 4   == 0) && \
  40.                      (((year) % 100 != 0) || ((year) % 400 == 0)))
  41.  
  42. int week(int, int, int);
  43. int weekday(int, int, int);
  44.  
  45. int main()
  46. {
  47.   int d, m, y;
  48.  
  49.   for (;;) {
  50.     printf("Enter day month year ... ");
  51.     scanf("%d %d %d", &d, &m, &y);              /* No error checking */
  52.     if (d < 1) break;
  53.     printf("%02d-%02d-%04d: week # %d\n", d, m, y, week(d, m, y));
  54.   }
  55.   exit(EXIT_SUCCESS);
  56. }
  57.  
  58. int week(
  59.   int day,              /* Returns the week in the year (1..52); day  */
  60.   int month,            /* month and year are assumed valid (no error */
  61.   int year              /* checking).  Gregorian calendar assumed.    */
  62. ){                      /* A week starts Sunday and ends in Saturday. */
  63.   int firstday;         /* Day of the week for Jan. 1, <year>         */
  64.   int yearday = day;    /* Day of the year for given date             */
  65.   static int monthdays[] = {
  66.      0, 31,  0, 31,             /* Dummy, Jan., Feb., Mar. */
  67.     30, 31, 30, 31,             /*  Apr., May,  Jun., Jul. */
  68.     31, 30, 31, 30              /*  Aug., Sep., Oct., Nov. */
  69.   };
  70.  
  71.   firstday = weekday(1, 1, year);
  72.   monthdays[2] = isleap(year) ? 29 : 28;
  73.   while (--month) {
  74.     yearday += monthdays[month];
  75.   }
  76.   printf("Yearday: %d\n", yearday);
  77.   return ((yearday + firstday - 1) / 7 + 1);
  78. }
  79.  
  80. int weekday(            /* Returns weekday: 0=Sun, 1=Mon, ..., 6=Sat */
  81.   int day,              /* Day of the month (1..31 or whatever),     */
  82.   int month,            /* month (1..12) and year; assumed valid (no */
  83.   int year              /* check done).  Gregorian calendar assumed. */
  84. ){
  85.   int century, result;
  86.  
  87.   if ((month -= 2) < 1) {
  88.     month +=12;
  89.     --year;
  90.   }
  91.   year -= (century = year / 100) * 100;
  92.   result = day + (13*month-1)/5 -2*century + year + year/4 + century/4;
  93.   if (result < 0) result = -result;
  94.   return (result % 7);
  95. }
  96. ---------------------------------End file: week.c
  97. --
  98. Maurizio Loreti                       http://mvxpd5.pd.infn.it/wwwcdf/mlo.html
  99. Un. of Padova, Dept. of Physics - Padova, Italy          loreti@padova.infn.it
  100.